<?php


namespace App\Api\Helpers;


use Symfony\Component\HttpFoundation\Response;

trait ApiResponse
{
    //1、封装通用方法
    /**
     * @param array $data
     * @param $code
     * @param $status
     * @param array $header
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/8 14:37
     */
    public function status(array $data, $code, $status, $header = [])
    {
        $response = [
            'code' => $code,
            'status' => $status,
            'data' => $data['data']
        ];
        return \Response::json($response, $code, $header);
    }

    //2、定义成功

    /**
     * @param $data
     * @param int $code
     * @param string $status
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/8 14:42
     */
    public function success($data = '操作成功!', $code = Response::HTTP_OK, $status = 'success')
    {
        return $this->status(compact('data'), $code, $status);
    }

    //3、定义失败

    /**
     * @param string $data
     * @param int $code
     * @param string $status
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/8 14:35
     */
    public function failed($data = 'Failed', $code = Response::HTTP_BAD_REQUEST, $status = 'failed')
    {
        return $this->status(compact('data'), $code, $status);
    }

    //4、定义未找到

    /**
     * @param string $data
     * @param int $code
     * @param string $status
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/8 14:35
     */
    public function notFound($data = 'Not Found!', $code = Response::HTTP_NOT_FOUND, $status = 'failed')
    {
        return $this->status(compact('data'), $code, $status);
    }

    //5、定义创建

    /**
     * @param string $data
     * @param int $code
     * @param string $status
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/8 14:35
     */
    public function created($data = 'Created', $code = Response::HTTP_CREATED, $status = 'success')
    {
        return $this->status(compact('data'), $code, $status);
    }

    //6、定义内部错误

    /**
     * @param string $data
     * @param int $code
     * @param string $status
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/8 14:36
     */
    public function internalError($data = 'Internal Error!', $code = Response::HTTP_INTERNAL_SERVER_ERROR, $status = 'failed')
    {
        return $this->status(compact('data'), $code, $status);
    }
}